package borge.web.teamspirit.client.servercomm;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import borge.web.teamspirit.client.interfaces.PersistentData;
import borge.web.teamspirit.client.interfaces.PersistentData.DataKey;


public class DBQueryResults implements Serializable {

    private HashMap<DataKey, Result> iResultMap;
    private Boolean iQuerySuccess;
    private String iQueryErrorMsg;
 
    public DBQueryResults(){
        iResultMap = new HashMap<DataKey, Result>();
        iQuerySuccess = true;
    }
    
    public DBQueryResults(boolean aSuccess, String aErrorMsg){
    	iQuerySuccess = aSuccess;
    	iQueryErrorMsg = aErrorMsg;
    }
    
    public void addResult(DataKey aKey, Result aResult){
    	iResultMap.put(aKey, aResult);
    }
    
    public int getNumResults(){
    	if(iResultMap == null)
    		return 0;
    	return iResultMap.size();
    }
    
    public Result getResult(DataKey aKey){
    	return iResultMap.get(aKey);
    }
    
    public boolean querySucceeded(){
    	return iQuerySuccess;
    }
    
    public String getErrorMsg(){
    	return iQueryErrorMsg;
    }
    
    public static class Result implements Serializable {
    	
        private boolean iSuccess;
        private String iErrorMsg;
        private List<PersistentData> iResultList;
    	
        public Result(){
        	
        }
    	// When we fail
        public Result(String aErrorMsg){
            iSuccess = false;
            iErrorMsg = aErrorMsg;
        }
        
        public Result(List<PersistentData> aList){
            iSuccess = true;
            iResultList = aList;
        }
        
        public Result(PersistentData aData){
        	iResultList = new ArrayList<PersistentData>();
        	iResultList.add(aData);
        }
        
        public Result(boolean status){
            iSuccess = status;
        }
        
        public void addResultData(Result aResult){
        	
        	List<PersistentData> list = aResult.getResultList();
        	
        	if(list != null && list.size() > 0){
        		
        		for(PersistentData pd : list)
        			addObject(pd);
        	}
        	
        }
        
        public void addObject(PersistentData aObject){
        	if(iResultList == null)
        		iResultList = new ArrayList<PersistentData>();
        	
        	iResultList.add(aObject);
        }
        
        public void setSuccess(boolean aSuccess){
        	iSuccess = aSuccess;
        }
        
        public boolean hasObjects(){
        	return iResultList != null && iResultList.size() > 0;
        }
        
        public boolean queryFailed(){
            return !iSuccess;
        }
        
        public boolean getBooleanState(){
        	return iSuccess;
        }
        
        public String getErrorMsg(){
            return iErrorMsg;
        }
        
        public List<PersistentData> getResultList(){
            return iResultList;
        }

    }
    

        
    /*
    public DBQueryResult setNext(DBQueryResult result){
    	iNext = result;
    	return iNext;
    }
    
    public DBQueryResult getNext(){
    	return iNext;
    }*/
    
}
